const fs = require('fs');
const path = require('path');

// 读取Appointment.ets文件
const filePath = 'd:\\harmonyprojects\\disease\\entry\\src\\main\\ets\\features\\patient\\Appointment.ets';

function checkAppointmentSyntax() {
  try {
    // 读取文件内容
    const content = fs.readFileSync(filePath, 'utf8');
    
    // 基础语法检查
    console.log('开始检查Appointment.ets文件语法...');
    
    // 检查关键结构
    const checks = [
      { pattern: /@Entry\s*\n*@Component/, message: '@Entry和@Component装饰器' },
      { pattern: /struct\s+Appointment/, message: 'Appointment结构体' },
      { pattern: /build\s*\(\)\s*{/, message: 'build方法' },
      { pattern: /@Builder\s+render/, message: '@Builder装饰器方法' },
      { pattern: /export\s+default\s+Appointment/, message: '导出语句' },
      { pattern: /interface\s+Department/, message: 'Department接口' },
      { pattern: /interface\s+Doctor/, message: 'Doctor接口' },
      { pattern: /interface\s+AppointmentInfo/, message: 'AppointmentInfo接口' },
    ];
    
    let allPassed = true;
    checks.forEach(check => {
      if (!check.pattern.test(content)) {
        console.log(`❌ 缺少: ${check.message}`);
        allPassed = false;
      } else {
        console.log(`✅ 找到: ${check.message}`);
      }
    });
    
    // 检查可能的语法错误
    const potentialErrors = [
      { pattern: /}\s*}\s*}/, message: '可能存在多余的右花括号' },
      { pattern: /{\s*{\s*{/, message: '可能存在嵌套过深的花括号' },
      { pattern: /\(\s*\)\s*\(\s*\)/, message: '可能存在多余的括号' },
    ];
    
    let hasWarnings = false;
    potentialErrors.forEach(check => {
      if (check.pattern.test(content)) {
        console.log(`⚠️  警告: ${check.message}`);
        hasWarnings = true;
      }
    });
    
    // 检查导入语句
    const requiredImports = [
      'import router from \'@ohos.router\';',
      'import promptAction from \'@ohos.promptAction\';'
    ];
    
    requiredImports.forEach(imp => {
      if (!content.includes(imp)) {
        console.log(`❌ 缺少必要导入: ${imp}`);
        allPassed = false;
      } else {
        console.log(`✅ 找到必要导入: ${imp}`);
      }
    });
    
    // 检查步骤流程
    const steps = [1, 2, 3, 4];
    steps.forEach(step => {
      const stepMethod = `renderStepIndicator|renderDepartmentSelection|renderDoctorSelection|renderTimeSelection|renderAppointmentForm|renderBottomActions`;
      if (!new RegExp(stepMethod).test(content)) {
        console.log(`❌ 缺少步骤渲染方法`);
        allPassed = false;
      }
    });
    
    // 文件长度检查
    const lineCount = content.split('\n').length;
    console.log(`文件包含 ${lineCount} 行`);
    
    if (allPassed && !hasWarnings) {
      console.log('\n🎉 Appointment.ets 文件语法检查通过，没有发现明显错误！');
    } else if (allPassed) {
      console.log('\n⚠️  Appointment.ets 文件基本结构正确，但有一些潜在警告需要注意。');
    } else {
      console.log('\n❌ Appointment.ets 文件可能存在语法问题，请检查上述错误。');
    }
    
    return allPassed;
  } catch (error) {
    console.error('读取或检查文件时出错:', error.message);
    return false;
  }
}

// 执行检查
const result = checkAppointmentSyntax();
process.exit(result ? 0 : 1);